<?php
class Admin_Model_Category extends Zend_Db_Table_Abstract{
	protected $_name = 'tblcategories';
	
	public function getAllCategory(){
        $sql = $this->_db->select()
                         ->from($this->_name)
                         ->where('1=1')
                         ->order('cat_id desc');
                                
        return $this->_db->fetchAll($sql);
    }
	
	public function getCategory($id){
        $sql = $this->_db->select()
                         ->from($this->_name)
                         ->where('cat_id='.$id);    
        return $this->_db->fetchRow($sql);   
    }
	 
	public function addCategory($catname,$catdes='',$catimg=''){
		$arrdata = array(
			'cat_name' => $catname,
            'cat_des'  => $catdes,
            'cat_img'=> $catimg
		);
		return $this->insert($arrdata);
	}
	public function editCategory($catname,$catdes='',$catimg='',$cond){
        
        $arrdata = array(
            'cat_name' => $catname,
            'cat_des'  => $catdes,
            'cat_img'=> $catimg
        );
        return $this->update($arrdata,'cat_id='.$cond);
    }
	/*
	public function deleteCategory($id){
	}
	*/
	
	//upload
	public function getExtension($str){
		$arrDot=explode('.',$str);// cat chuoi ra thanh mang voi ki tu .
		$extension=end($arrDot);// lay fan tu cuoi trong mang
		return $extension;
	}
	
	public function uploadFile($file,$url,$arrAllowEx,$maxSize){
		$fName=strtolower($file['name']);// lay ten file duoc upload len vidu: banner.gif
		$tmpfName=$file['tmp_name'];// file luu tru tam thoi cua file dc upload len 
		$fSize=$file['size'];// dung luong cua file duoc upload
		$extension=$this->getExtension($fName);
		if(!in_array($extension, $arrAllowEx)){
			return "-3"; //phan mo rong khong hop le
		}
		if($fSize>$maxSize){
			return "-2";// Kick thuoc file vuot qua qui dinh
		}
		$fNewName=time().'_'.$fName;
		$url=$url.$fNewName;
		if(move_uploaded_file($tmpfName,$url)){
			@chmod($fNewName,776);
			return $fNewName;
		}
	}
	
}